<?php

require_once('config/maptrick.conf.php');
require_once(SITE_ROOT . 'lib/SmartyMaptrick.class.php');
require_once(SITE_ROOT . 'class/logic/Registration.class.php');
require_once(SITE_ROOT . 'class/logic/LogicHoC.class.php');
require_once(SITE_ROOT . 'lib/Session.php');
require_once(SITE_ROOT . 'lib/SmartyPaginate.class.php');

	$logic = LogicHoC::getInstance();
	$smarty = new SmartyMaptrick();
	SmartyPaginate::connect();
	
	$directlink = "http://" . $_SERVER['HTTP_HOST'] . "/hallofcool.php?";
	
	if ((!isset($_GET['index']) && count($_GET) > 0) || (isset($_GET['index']) && count($_GET) > 1)) {
		if (isset($_GET['country'])) $countryID = $_GET['country'];
		if (isset($_GET['division'])) $divisionID = $_GET['division'];
		if (isset($_GET['league'])) $leagueID = $_GET['league'];
		if (isset($_GET['alliance'])) $allianceID = $_GET['alliance'];
		if (isset($_GET['month'])) $month = $_GET['month'];
		if (isset($_GET['year'])) $year = $_GET['year'];
		if (isset($_GET['term'])) $term = $_GET['term'];
		if (isset($_GET['type'])) $type = $_GET['type'];
		SmartyPaginate::reset();
	}
	else {
		if (isset($_SESSION['country'])) $countryID = $_SESSION['country'];	
		if (isset($_POST['select_countries'])) $countryID = $_POST['select_countries'];
		
		if (isset($_SESSION['division'])) $divisionID = $_SESSION['division'];
		if (isset($_POST['select_divisions'])) $divisionID = $_POST['select_divisions'];
		
		if (isset($_SESSION['league'])) $leagueID = $_SESSION['league'];
		if (isset($_POST['select_leagues'])) $leagueID = $_POST['select_leagues'];
		
		if (isset($_SESSION['alliance'])) $allianceID = $_SESSION['alliance'];
		if (isset($_POST['select_alliances'])) $allianceID = $_POST['select_alliances'];
		
		if (isset($_SESSION['month'])) $month = $_SESSION['month'];
		if (isset($_POST['select_months'])) $month = $_POST['select_months'];
		
		if (isset($_SESSION['year'])) $year = $_SESSION['year'];
		if (isset($_POST['select_years'])) $year = $_POST['select_years'];
		
		if (isset($_SESSION['term'])) $term = $_SESSION['term'];
		if (isset($_POST['select_terms'])) $term = $_POST['select_terms'];
		
		if (isset($_SESSION['type'])) $type = $_SESSION['type'];
		if (isset($_POST['select_types'])) $type = $_POST['select_types'];	
	}
	
	if ($countryID == 0) {
		unset($_SESSION['country']);
		unset($countryID);
	}
	else {
		$_SESSION['country'] = $countryID;
		$directlink .= "country=" . $countryID . "&amp;";
	}
	
	if ($divisionID == 0) {
		unset($_SESSION['division']);
		unset($divisionID);
	}
	else {
		$_SESSION['division'] = $divisionID;
		$directlink .= "division=" . $divisionID . "&amp;";
	}
	
	if ($leagueID == 0) {
		unset($_SESSION['league']);
		unset($leagueID);
	}
	else {
		$_SESSION['league'] = $leagueID;
		$directlink .= "league=" . $leagueID . "&amp;";
	}
	
	if ($allianceID == 0) {
		unset($_SESSION['alliance']);
		unset($allianceID);
	}
	else {
		$_SESSION['alliance'] = $allianceID;
		$directlink .= "alliance=" . $allianceID . "&amp;";
	}
	
	if ($month == 0) {
		unset($_SESSION['month']);
		unset($month);
	}
	else {
		$_SESSION['month'] = $month;
		$directlink .= "month=" . $month . "&amp;";
	}
	
	if ($year == 0) {
		unset($_SESSION['year']);
		unset($year);
	}
	else {
		$_SESSION['year'] = $year;
		$directlink .= "year=" . $year . "&amp;";
	}
	
	if (!isset($term)) {
		$term = CONST_COOL;
	}
	$_SESSION['term'] = $term;
	$directlink .= "term=" . $term . "&amp;";
		
	if (!isset($type)) {
		$type = CONST_AWAY;
	}
	$_SESSION['type'] = $type;
	$directlink .= "type=" . $type . "&amp;";
	
	$hidden = $_POST['hidden'];
	if ($hidden == 'login') {
		try {
			$register = new Registration;
			$register->login($_POST['user'], $_POST['password']);
		}
		catch (Exception $e) {
			$smarty->assign('message', $e->getMessage());
		}
	}
	else if ($hidden == 'logout') {
		try {
			$register = new Registration;
			$register->logout();
		}
		catch (Exception $e) {}
	}
	else if ($hidden == 'update') {
		try {
			$logic->update($_POST['teamID']);
		}
		catch (IsBotException $ibe) {
			$smarty->assign('warning', "You don't really want to get the coolness ratings from a bot, do you? If the former owner was in the Hall of Cool he isn't anymore now.");
		}
		catch (LeagueNotAvailableException $lnae) {
			$smarty->assign('error', "The league the team is playing in couldn't be determinated. It wasn't saved/updated in the Hall of Cool. Please try again later!");
		}
		catch (Exception $e) {
			unset($_SESSION['login']);
			unset($_SESSION['login_teamID']);
			unset($_SESSION['HT']);
			$smarty->assign('message', 'Timeout!');
		}
	}
	else if ($hidden == 'show') {
		SmartyPaginate::reset();
	}
			
	$smarty->assign('action', $_SERVER['PHP_SELF']);
	$smarty->assign('loggedIn', $_SESSION['login']);

	// assign the important attributes for the header
	$smarty->assign('title', 'Maptrick');
	$smarty->assign('home_active', '');
	$smarty->assign('cool_active', '');
	$smarty->assign('hoc_active', 'active');
	
	$smarty->assign_by_ref('countries', $logic->getCountries());
	$smarty->assign_by_ref('divisions', $logic->getDivisions());
	
	if (isset($leagueID)) {
		$league = $logic->getLeague($leagueID);
		if (isset($league)) {
			$smarty->assign('selected_country', $league->getDivision()->getCountry()->getCountryID());
			$smarty->assign_by_ref('divisions', $logic->getDivisions($league->getDivision()->getCountry()->getCountryID()));
			$smarty->assign('selected_division', $league->getDivision()->getDivisionID());
			$smarty->assign_by_ref('leagues', $logic->getLeagues($league->getDivision()->getCountry()->getCountryID(), $league->getDivision()->getDivisionID()));
			$smarty->assign('selected_league', $league->getLeagueID());
		}
	}
	else if (isset($countryID) && isset($divisionID)) {
		$smarty->assign('selected_country', $countryID);
		$smarty->assign_by_ref('divisions', $logic->getDivisions($countryID));
		$smarty->assign('selected_division', $divisionID);
		$smarty->assign_by_ref('leagues', $logic->getLeagues($countryID, $divisionID));
	}
	else if (isset($countryID)) {
		$smarty->assign('selected_country', $countryID);
		$smarty->assign_by_ref('divisions', $logic->getDivisions($countryID));
	}
	else if (isset($divisionID)) {
		$smarty->assign_by_ref('divisions', $logic->getDivisions());
		$smarty->assign('selected_division', $divisionID);
	}
	
	$smarty->assign('selected_alliance', $allianceID);
	$smarty->assign_by_ref('alliances', $logic->getAlliances());
	
	$smarty->assign('selected_month', $month);
	$smarty->assign('months', $logic->getMonths());
	$smarty->assign('selected_year', $year);
	$smarty->assign('years', $logic->getYears($month));
	
	$smarty->assign('selected_term', $term);
	$smarty->assign('selected_type', $type);
	
	$resultSize = $logic->getRankingCount($countryID, $divisionID, $leagueID, $allianceID, $month, $year);
	
	SmartyPaginate::setUrlVar("index");
	SmartyPaginate::setLimit(100);
	SmartyPaginate::setPageLimit(5);
	SmartyPaginate::setTotal($resultSize);
	SmartyPaginate::assign($smarty);

    $smarty->assign('resultSize', $resultSize);
	$smarty->assign_by_ref('hocTable', $logic->createHoCTable($term, $type, $countryID, $divisionID, $leagueID, $allianceID, $month, $year, SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit()));
	
	$smarty->assign('login_teamID' , $_SESSION['login_teamID']);
	
	$smarty->assign('directlink', substr($directlink, 0, strlen($directlink) - 5));
	
	// display it
	$smarty->display('header.tpl');
	$smarty->display('hallofcool.tpl');	
	$smarty->display('footer.tpl');
	
?>